/***************************************************************************
 * 文件名:ETSServiceImpl.java<br>
 * 版本: <br>
 * 描述: <br>
 * 版权所有: <br>
 * //////////////////////////////////////////////////////// <br>
 * 创建者: robo<br>
 * 创建日期: 2010-11-15 <br>
 * 修改者: <br>
 * 修改日期: <br>
 * 修改说明: <br>
 **************************************************************************/
package com.sunwayhorizo.health.ets.base.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;

import org.jboss.seam.Component;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.log.Log;

import com.sunwayhorizo.health.business.dto.DictionaryDTO;
import com.sunwayhorizo.health.business.dto.ResultsDTO;
import com.sunwayhorizo.health.common.BusinessConstants;
import com.sunwayhorizo.health.common.StrUtil;
import com.sunwayhorizo.health.ets.base.persistent.EtCodingSchemes;
import com.sunwayhorizo.health.ets.base.persistent.EtConceptlists;
import com.sunwayhorizo.health.ets.base.persistent.EtConcepts;
import com.sunwayhorizo.health.ets.base.persistent.EtDescriptions;
import com.sunwayhorizo.health.ets.base.persistent.EtVersions;
import com.sunwayhorizo.health.ets.exceptions.ETSBadParameterException;
import com.sunwayhorizo.health.ets.exceptions.ETSDataException;
import com.sunwayhorizo.health.ets.exceptions.ETSNotFoundException;
import com.sunwayhorizo.health.ets.exceptions.common.ETSDAOException;
import com.sunwayhorizo.health.hl7.common.Hl7Exception;

@Name("ETSService")
@Stateless
public class ETSServiceImpl implements ETSService {

	EtsFactory factory = new EtsFactoryImpl();

	ResultsDTO dto = new ResultsDTO();

	@Logger
	Log log;

	public EtCodingSchemes findCodingScheme(String codingschemeEnName)
			throws ETSNotFoundException, ETSBadParameterException,
			ETSDataException {
		return factory.getICodingSchemesDao().getCodingSchemeForName(
				codingschemeEnName);
	}

	public EtVersions findCodingSchemeVersion(String codingSchemesID)
			throws ETSNotFoundException, ETSBadParameterException,
			ETSDataException {
		return factory.getICodingSchemesVersionsDao().getEtVersions(
				codingSchemesID);

	}

	public EtConcepts findConcept(String conceptId)
			throws ETSNotFoundException, ETSBadParameterException,
			ETSDataException {
		return factory.getIConceptsDao().find(EtConcepts.class, conceptId);
	}

	public EtDescriptions findDescription(String s)
			throws ETSNotFoundException, ETSBadParameterException,
			ETSDataException {
		return null;
	}

	public EtConceptlists getConceptList(String s) throws ETSNotFoundException,
			ETSBadParameterException, ETSDataException {
		return null;
	}

	public EtConceptlists getConceptListByID(String s)
			throws ETSBadParameterException, ETSNotFoundException,
			ETSDataException {
		return null;

	}

	public List<EtConceptlists> getMatchingConceptLists(String s)
			throws ETSDataException, ETSBadParameterException {
		return null;

	}

	public List<EtConceptlists> getAllConceptLists() throws ETSDataException {
		return null;
	}

	public EtCodingSchemes getCodingScheme(String codingSchemeId)
			throws ETSNotFoundException, ETSDataException,
			ETSBadParameterException {
		return factory.getICodingSchemesDao().find(EtCodingSchemes.class,
				codingSchemeId);
	}

	public EtCodingSchemes getCodingSchemeByOID(String s)
			throws ETSNotFoundException, ETSDataException,
			ETSBadParameterException {
		return factory.getICodingSchemesDao().getCodingSchemeByOID(s);
	}

	public List<EtCodingSchemes> getMatchingCodingSchemes(String s)
			throws ETSDataException, ETSBadParameterException {
		return null;
	}

	public List<EtCodingSchemes> getAllCodingSchemes() throws ETSDataException {
		return factory.getICodingSchemesDao().getAllCodingSchemes();
	}

	public List<EtConceptlists> getAllConceptLists(int i)
			throws ETSDataException, ETSBadParameterException {
		return null;
	}

	public String[] getConceptListGroupNames() throws ETSDataException {
		return null;
	}

	public EtConceptlists getConceptList(String s, String s1)
			throws ETSBadParameterException, ETSNotFoundException,
			ETSDataException {
		return null;
	}

	public List<EtConceptlists> getConceptListsByGroupName(String s)
			throws ETSBadParameterException, ETSDataException {
		return null;
	}

	public List<DictionaryDTO> narrowConceptResults(String as[], String s,
			boolean flag) throws ETSBadParameterException, ETSDataException {
		return null;
	}

	public List<DictionaryDTO> findConcepts(String as[], boolean flag)
			throws ETSBadParameterException, ETSDataException {
		return factory.getIConceptsDao().getBulkConcepts(as, null, flag);
	}

	public List<DictionaryDTO> findConcepts(String as[], int i)
			throws ETSBadParameterException, ETSDataException {
		return null;
	}

	public List<DictionaryDTO> findConcepts(String as[],
			String codingSchemesEnName, int i) throws ETSBadParameterException,
			ETSDataException {
		return null;
	}

	public List<DictionaryDTO> findConcepts(String codingSchemesEnName)
			throws ETSBadParameterException, ETSDataException {
		return factory.getIConceptsDao()
				.getConceptsObjects(codingSchemesEnName);

	}

	public List<DictionaryDTO> findConcepts(String codingSchemesChName,
			String codingSchemesEnName, String versionName)
			throws ETSBadParameterException, ETSDataException {
		return factory.getIConceptsDao().getConceptsObjects(
				codingSchemesChName, codingSchemesEnName, versionName);

	}
	
	public ResultsDTO findDictionaryDtosByCSNameAndVersionNameAndConceptCodeAndModelCode(String codingSchemesEnName,String versionName,String conceptCode,String modelCode) 
    throws ETSBadParameterException, ETSDataException {
        List<DictionaryDTO> result = factory.getIConceptsDao().findDictionaryDtosByCSNameAndVersionNameAndConceptCodeAndModelCode(codingSchemesEnName, versionName, conceptCode,modelCode);
        dto.setResultFlag(true);
        dto.setObject(result);
        dto.setResultDesc("查询到数据记录");
        return dto;
	}
	
	@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
	public List<DictionaryDTO> findDictionaryDtosListByCond(String codingSchemesEnName,String versionName,String conceptCode,String conceptClass) 
        throws Hl7Exception {
        return factory.getIConceptsDao().findDictionaryDtosListByCond(codingSchemesEnName, versionName, conceptCode,conceptClass);
    }

	public ResultsDTO findConceptsList(String codingSchemesChName,
			String codingSchemesEnName, String versionName)
			throws ETSBadParameterException, ETSDataException {
		List<DictionaryDTO> list = factory.getIConceptsDao()
				.getConceptsObjects(codingSchemesChName, codingSchemesEnName,
						versionName);
		dto.setObject(list);
		dto.setResultFlag(true);
		dto.setResultDesc("查询到数据记录");
		return dto;

	}

	public List<DictionaryDTO> findConcepts(String codingSchemesEnName,
			String versionName) throws ETSBadParameterException,
			ETSDataException {
		return factory.getIConceptsDao().getConceptsObjects(
				codingSchemesEnName, versionName);

	}

	public ResultsDTO findConceptsList(String codingSchemesEnName,
			String versionName) throws ETSBadParameterException,
			ETSDataException {
		List<DictionaryDTO> list = null;
		ResultsDTO rdto = new ResultsDTO();
		if ((codingSchemesEnName == null || codingSchemesEnName.equals(""))
				&& (versionName == null || versionName.equals(""))) {
			list = new ArrayList<DictionaryDTO>();
			rdto.setResultFlag(true);
			rdto.setObject(list);
			rdto.setResultDesc("两个参数为空不能查询到记录");
			log.info("==================查询参数都为空不查询数据==================");
		} else {
			list = factory.getIConceptsDao().getConceptsObjects(
					codingSchemesEnName, versionName);
			rdto.setResultFlag(true);
			rdto.setObject(list);
			rdto.setResultDesc("查询到数据记录");
			log.info("==================返回总数据条数为==================:"
					+ list.size());
		}
		return rdto;
	}

	public ResultsDTO findConceptsListAsXml(String codingSchemesEnName, String versionName) throws ETSBadParameterException, ETSDataException
    {
        String xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
        ResultsDTO rdto = new ResultsDTO();
        if (StrUtil.isEmpty(codingSchemesEnName) )
        {
            rdto.setResultFlag(true);
            rdto.setObject(xml);
            rdto.setResultDesc("术语字典英文名称不能为空!");
            log.info("==================术语字典英文名称为空不查询数据==================");
        }
        else
        {
            xml = factory.getIConceptsDao().getSimpleConceptsObjects(codingSchemesEnName, versionName);
            if( StrUtil.isEmpty(xml))
            {
                rdto.setResultFlag(false);
                rdto.setResultDesc("请维护术语项!");
            }
            else{
                rdto.setResultFlag(true);
                rdto.setResultDesc("查询成功!");
                rdto.setObject(xml);
            }
            //log.info("==================返回xml为==================:" + xml);
        }
        return rdto;
    }
	
	public DictionaryDTO findConceptsObj(String codingSchemesEnName,
			String conceptCode) throws ETSBadParameterException,
			ETSDataException {
		return factory.getIConceptsDao().getConceptsObject(codingSchemesEnName,
				conceptCode);
	}
	
//	public DictionaryDTO findConceptsByConceptNameAndCodingSchemesEnName(String codingSchemesEnName,
//			String conceptName) throws ETSBadParameterException,
//			ETSDataException {
//		return factory.getIConceptsDao().getConceptsByConceptNameAndCodingSchemesEnName(codingSchemesEnName,
//				conceptName);
//	}

	public ResultsDTO findConceptsOut(String codingSchemesEnName,
			String conceptCode) throws ETSBadParameterException,
			ETSDataException {
		DictionaryDTO dicDto = factory.getIConceptsDao().getConceptsObject(
				codingSchemesEnName, conceptCode);
		dto.setObject(dicDto);
		dto.setResultFlag(true);
		dto.setResultDesc("查询到数据记录");
		return this.dto;
	}

	public List<EtConceptlists> getMatchingConceptListsByDescription(String s,
			String s1) throws ETSDataException, ETSBadParameterException {
		return null;
	}

	public boolean isEquivalent(String s, String s1, int i)
			throws ETSDataException, ETSNotFoundException,
			ETSBadParameterException {
		return true;
	}

	public String[] getEquivalentConceptIDs(String s, int i)
			throws ETSDataException, ETSNotFoundException,
			ETSBadParameterException {
		return null;
	}

	public boolean validateConceptListMembership(String s, String s1, String s2)
			throws ETSDataException, ETSBadParameterException,
			ETSNotFoundException {
		return true;
	}

	public boolean isEquivalent(String s, String s1, int i, String s2,
			boolean flag) throws ETSDataException, ETSNotFoundException,
			ETSBadParameterException {
		return true;
	}

	public String[] getEquivalentConceptIDs(String s, int i, String s1,
			boolean flag) throws ETSDataException, ETSNotFoundException,
			ETSBadParameterException {
		return null;
	}

	public String[] getEquivalentConceptIDs(String s, int i, String s1,
			boolean flag, String s2) throws ETSDataException,
			ETSNotFoundException, ETSBadParameterException {
		return null;
	}

	// public LocalConceptDescription[]
	// findLocallyPreferredDescriptionsForConceptIDs(
	// String as[]) throws ETSDataException, ETSBadParameterException {
	// return null;
	// }
	//
	// public TerminologyConceptDescription[]
	// findTerminologyPreferredDescriptionsForConceptIDs(
	// String as[]) throws ETSDataException, ETSBadParameterException {
	// return null;
	// }

	public List<EtDescriptions> findDescriptionsForConceptIDs(String as[])
			throws ETSDataException, ETSBadParameterException {
		return null;
	}

	public List<DictionaryDTO> findMatchingConceptsByDescriptionTerm(String s)
			throws ETSDataException, ETSBadParameterException {
		return null;
	}

	public List<DictionaryDTO> findMatchingConceptsByDescriptionTerm(String s,
			long l) throws ETSDataException, ETSBadParameterException {
		return null;
	}

	// public ConceptListMember[] findConceptListMembers(String as[],
	// String as1[], String as2[], int i, boolean flag)
	// throws ETSBadParameterException, ETSDataException {
	// return null;
	// }

	public Date getSystemDate() throws ETSDAOException {
		return null;
	}

	public Date getMaxDate() throws ETSDAOException {
		return null;
	}

	public ResultsDTO findConceptsPlaceList(String code)
			throws ETSBadParameterException, ETSDataException {

		String codingSchemeEnName = BusinessConstants.NAMEPART_DIC_JIGUAN;

		log.info("传入参数为：======================:" + code);
		if (code != null && !code.equals("")) {
			String province = code.substring(2, 6);
			String city = code.substring(4, 6);
			if (province.equals("0000")) {
				code = code.substring(0, 2);
			} else {
				if (city.equals("00")) {
					code = code.substring(0, 4);
				}
			}
		}
		List<DictionaryDTO> list = factory.getIConceptsDao().getPlace(
				codingSchemeEnName, code);
		log.info("返回总数据条数为==================:" + list.size());
		ResultsDTO resultsDTO = new ResultsDTO();
		resultsDTO.setResultFlag(true);
		resultsDTO.setObject(list);
		resultsDTO.setResultDesc("查询到数据记录");
		return resultsDTO;
	}

	
	public DictionaryDTO findConceptsDTO(String conceptCode, String conceptName)
			throws ETSBadParameterException, ETSDataException {
		return factory.getIConceptsDao().getConceptsDTO(conceptCode,
				conceptName);
	}

	public DictionaryDTO findConceptsDTOByCodeAndEnName(
			String codingSchemesEnName, String conceptName)
			throws ETSBadParameterException, ETSDataException {
		return factory.getIConceptsDao().getConceptsDTOByCodeAndEnname(codingSchemesEnName,
				conceptName);
	}
	
	public DictionaryDTO findDictionaryDTOBlobOrClob(String codingschemeEnName,
			String conceptCode)throws ETSBadParameterException, ETSDataException {
		return factory.getIConceptsDao().getConceptsDTOBlobOrClob(codingschemeEnName, conceptCode);
	}
	
	public ResultsDTO findConceptsListBlobOrClob(String codingSchemesChName,
			String codingSchemesEnName, String versionName)
			throws ETSBadParameterException, ETSDataException {
		List<DictionaryDTO> list = factory.getIConceptsDao()
				.getBlobOrClob(codingSchemesChName, codingSchemesEnName,
						versionName);
		dto.setObject(list);
		dto.setResultFlag(true);
		dto.setResultDesc("查询到数据记录");
		return dto;
	}
	
	public ResultsDTO findDictionaryDtosByConceptClass(String conceptClass) throws ETSBadParameterException, ETSDataException {
        List<DictionaryDTO> result = factory.getIConceptsDao().findDictionaryDtosByConceptClass(conceptClass);
        dto.setResultFlag(true);
        dto.setObject(result);
        dto.setResultDesc("查询到数据记录");
        return dto;
    }
	
	public List<DictionaryDTO> findDictionaryDtosListByConceptClass(String conceptClass) throws Hl7Exception {
        List<DictionaryDTO> result = factory.getIConceptsDao().findDictionaryDtosByConceptClass(conceptClass);
        return result;
    }
	
	public ResultsDTO findLeafDirAll(String codingschemeEnName,String versionName,String conceptCode,String conceptClass) throws ETSBadParameterException, ETSDataException {
	    if( StrUtil.isEmpty(codingschemeEnName) ){
	        dto.setResultFlag(false);
	        dto.setObject(null);
	        dto.setResultDesc("此服务不支持字典的英文名为空");
	        return dto;
        }
        List<DictionaryDTO> result = factory.getIConceptsDao().findLeafDirAll(codingschemeEnName, versionName, conceptCode, conceptClass);
        dto.setResultFlag(true);
        dto.setObject(result);
        dto.setResultDesc("查询到数据记录");
        return dto;
    }
	
	public ResultsDTO findLeafDirCodeAll(String codingschemeEnName,String versionName,String conceptCode,String conceptClass) throws ETSBadParameterException, ETSDataException {
        if( StrUtil.isEmpty(codingschemeEnName) ){
            dto.setResultFlag(false);
            dto.setObject(null);
            dto.setResultDesc("此服务不支持字典的英文名为空");
            return dto;
        }
        List<String> result = factory.getIConceptsDao().findLeafDirCodeAll(codingschemeEnName, versionName, conceptCode, conceptClass);
        dto.setResultFlag(true);
        dto.setObject(result);
        dto.setResultDesc("查询到数据记录");
        return dto;
    }
	
	@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
	public List<String> findLeafDirCode(String codingschemeEnName,String versionName,String conceptCode,String conceptClass) throws Hl7Exception
	{
	    EtConceptsManagerLocal etConceptsManager = (EtConceptsManagerLocal) Component.getInstance("EtConceptsManager");
	      List<String> list = etConceptsManager.findLeafDirCodeAll(codingschemeEnName,versionName,conceptCode,conceptClass);
	      return list;
	}
}
